package com.tgr.shortLink.project.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tgr.shortLink.project.dao.entity.LinkLocaleStatsDO;
import com.tgr.shortLink.project.dao.entity.LinkOsStatsDO;
import com.tgr.shortLink.project.dto.req.LinkStatsReqDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.HashMap;
import java.util.List;

public interface LinkOsStatsMapper extends BaseMapper<LinkOsStatsDO> {
    @Insert("INSERT INTO t_link_os_stats (gid, full_short_url, date,cnt,os,create_time, update_time, del_flag) " +
            " VALUES ( #{linkOsStats.gid}, #{linkOsStats.fullShortUrl}, #{linkOsStats.date}, #{linkOsStats.cnt},#{linkOsStats.os},NOW(),NOW(),0) " +
            "ON DUPLICATE KEY  UPDATE cnt = cnt+ #{linkOsStats.cnt};")
    void setLinkOsStats(@Param("linkOsStats") LinkOsStatsDO linkOsStatsDO);


    /**
     * 根据短链接获取指定日期内操作系统监控数据
     */
    @Select("SELECT " +
            "    os, " +
            "    SUM(cnt) AS count " +
            "FROM " +
            "    t_link_os_stats " +
            "WHERE " +
            "    full_short_url = #{param.fullShortUrl} " +
            "    AND gid = #{param.gid} " +
            "    AND date BETWEEN #{param.startDate} and #{param.endDate} " +
            "GROUP BY " +
            "    full_short_url, gid, os;")
    List<HashMap<String, Object>> listOsStatsByShortLink(@Param("param") LinkStatsReqDTO requestParam);


}
